const { addForm, getFormByAdminId, updateFormStatus, getFormByUserId, getFormByProjectId } = require('../dao/forms')

//发布项目
function FormAdd(params) {
  return new Promise(async (resolve, reject) => {
    const { project_id, user_id } = params
    console.log(params)
    if (!project_id || !user_id) {
      resolve(error('参数不全'))
    }
    addForm(params).then(() => {
      resolve(ok())
    }).catch(err => {
      resolve(error(err))
    })
  })
}

//根据老师id获取表单
function GetFormByAdminId(params) {
  return new Promise(async (resolve, reject) => {
    const { admin_id } = params
    if (!admin_id) {
      resolve(error('参数不全'))
    }
    getFormByAdminId(params).then((data) => {
      resolve(ok({ data: { list: data } }))
    }).catch(err => {
      resolve(error(err))
    })
  })
}

//更新表单状态
function UpdateFormStatus(params) {
  return new Promise(async (resolve, reject) => {
    const { form_id, status } = params
    if (!form_id || !status) {
      resolve(error('参数不全'))
    }
    updateFormStatus(params).then(() => {
      resolve(ok())
    }).catch(err => {
      resolve(error(err))
    })

  })
}

//根据用户id获取报名项目
function GetFormByUserId(params) {
  return new Promise(async (resolve, reject) => {
    const { user_id } = params
    if (!user_id) {
      resolve(error('参数不全'))
    }
    getFormByUserId(params).then((data) => {
      resolve(ok({ data: { list: data } }))
    }).catch(err => {
      resolve(error(err))
    })
  })
}

// 根据项目id获取全部报名表
function GetFormByProjectId(params) {
  return new Promise(async (resolve, reject) => {
    const { project_id } = params
    if (!project_id) {
      resolve(error('参数不全'))
    }
    getFormByProjectId(params).then((data) => {
      resolve(ok({ data: { list: data } }))
    }).catch(err => {
      resolve(error(err))
    })
  })
}



module.exports = {
  FormAdd, GetFormByAdminId, UpdateFormStatus, GetFormByUserId, GetFormByProjectId
}